package com.xm.servlet.reception_desk.search;


import com.xm.service.reception_desk.CommodityService;
import com.xm.util.DecryptExampleUtil;
import lombok.SneakyThrows;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 搜索页面
 *
 * @author 吴添文
 * @date 2023-12-26
 */
@WebServlet("/search")
public class SearchServlet extends HttpServlet {
    private final CommodityService service = new CommodityService();

    @SneakyThrows(Exception.class)
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
        HttpSession session = req.getSession(false);
        // 获取会话中存储的CSRF令牌
        String sessionToken = null;
        try {
            sessionToken = (String) session.getAttribute("token");
        } catch (Exception e) {
            System.out.println("没有登陆");
        }
        //AES解密
        String informationId = DecryptExampleUtil.decrypt(req.getParameter("id"));
        System.out.println("解密结果：" + informationId);

        //是否为空
        String isNotNull = "";
        String sortColor = req.getParameter("sortColor");
        String searchInput = req.getParameter("searchInput");

        String pageNumberStr = req.getParameter("pageNumber");
        String pageSizeStr = req.getParameter("pageSize");
        String order = req.getParameter("order");
        String classIdStr = req.getParameter("classId");
        //值是否为空
        String isAsc = req.getParameter("isAsc");

        //页码
        Integer pageNumber = pageNumberStr == null || pageNumberStr.equals(isNotNull) ? 1 :
                Integer.parseInt(pageNumberStr);
        //页数
        Integer pageSize = pageSizeStr == null || pageSizeStr.equals(isNotNull) ? 20 :
                Integer.parseInt(pageSizeStr);
        Integer classId = classIdStr == null || classIdStr.equals(isNotNull) ? 0 :
                Integer.parseInt(classIdStr);

        //传值
        req.setAttribute("commodityPage", service.searchNameOrKeywordWithPagination(req.getParameter("searchInput"),
                pageNumber,
                pageSize,
                order,
                isAsc == null || isAsc.equals(isNotNull),
                classId));
        req.setAttribute("searchInput", searchInput == null || searchInput.equals(isNotNull) ? "" : searchInput);
        req.setAttribute("sortColor", sortColor == null || sortColor.equals(isNotNull) ? 1 :
                Integer.parseInt(sortColor));
        // 验证CSRF令牌
        if (sessionToken != null) {
            req.setAttribute("accountId", req.getParameter("accountId"));
            req.setAttribute("name", req.getParameter("name"));
            req.setAttribute("id", informationId);
        }else{
            req.setAttribute("accountId", "0");
            req.setAttribute("name", "null");
            req.setAttribute("id", -1);
            // 令牌失效
            req.setAttribute("status", false);
            System.out.println("CSRF令牌验证失败");
        }
        req.getRequestDispatcher("/WEB-INF/page/reception_desk/search.jsp").forward(req, resp);
    }
}
